package org.example.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import org.apache.catalina.User;
import org.example.domain.SysUser;
import org.example.domain.SysUserPost;
import org.example.domain.SysUserRole;
import org.example.mapper.SysUserMapper;
import org.example.mapper.SysUserPostMapper;
import org.example.mapper.SysUserRoleMapper;
import org.example.service.SysUserRoleService;
import org.example.service.SysUserService;
import org.example.vo.SysUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    SysUserPostMapper sysUserPostMapper;
    /**
     * 用户名密码
     * @param userName
     * @return
     */
    @Override
    public SysUser getOne(String userName) {
        return sysUserMapper.getOne(userName);
    }

    @Override
    public List<SysUser> list(SysUserVo sysUserVo) {
        return sysUserMapper.list(sysUserVo);
    }

    @Override
    public List<SysUser> listUserMenu() {
        return sysUserMapper.listUserMenu();
    }

    @Override
    @Transactional
    public boolean saveUser(@RequestBody SysUser sysUser) {
        sysUser.setCreateTime(new Date());
        sysUser.setPassword(DigestUtil.md5Hex(sysUser.getPassword()+"2110a"));
        sysUserMapper.saveUser(sysUser);
        System.err.println(sysUser);
        Long[] rids = sysUser.getRids();
//        List<SysUserRole> list = new ArrayList<>();
        //角色中间表
        if(rids.length>0 && rids!=null){
            for (Long rid : rids) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getUserId());
                sysUserRole.setRoleId(rid);
                sysUserRoleMapper.insertSysUserId(sysUserRole);
//                list.add(sysUserRole);
            }
//            sysUserRoleMapper.insertSysUserBath(list);
        }
        //岗位中间表
        Long[] postsId = sysUser.getPostsId();
        if(postsId.length>0 && postsId!=null){
            for (Long post : postsId) {
                SysUserPost sysUserPost = new SysUserPost();
                sysUserPost.setUserId(sysUser.getUserId());
                sysUserPost.setPostId(post);
                sysUserPostMapper.insertsysUserPostId(sysUserPost);
            }
        }

        return true;
    }

    @Override
    @Transactional
    public boolean updateUser(@RequestBody SysUser sysUser) {
        sysUser.setCreateTime(new Date());
        sysUserMapper.updateUser(sysUser);
        Long[] rids = sysUser.getRids();
        if(rids.length>0 && rids!=null){
            for (Long rid : rids) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(sysUser.getUserId());
                sysUserRole.setRoleId(rid);
                sysUserRoleMapper.updateSysUserId(sysUserRole);
            }
        }
        //岗位中间表
        Long[] postsId = sysUser.getPostsId();
        if(postsId.length>0 && postsId!=null){
            for (Long post : postsId) {
                SysUserPost sysUserPost = new SysUserPost();
                sysUserPost.setUserId(sysUser.getUserId());
                sysUserPost.setPostId(post);
                sysUserPostMapper.updateSysUserPostId(sysUserPost);
            }
        }
        return true;
    }

    @Override
    public SysUser selectUserById(Long userId) {
        return sysUserMapper.selectUserById(userId);
    }

    @Override
    public  List<Long> rolesById(Long userId) {
        return sysUserMapper.rolesById(userId);
    }

    @Override
    @Transactional
    public boolean delAll(List<Long> userId) {
        return sysUserMapper.delAll(userId);
    }

    @Override
    public List<SysUser> listExcel() {
        return sysUserMapper.listExcel();
    }

    @Override
    public List<Long> postById(Long userId) {
        return sysUserMapper.postById(userId);
    }

    @Override
    @Transactional
    public boolean updateUsers(List<Long> userId) {
        sysUserMapper.updateUsers(userId);
        sysUserMapper.delAll(userId);
        return true;
    }


}
